# stoker_resolvers: tracking module for machine resolvers
#
# Author:   Mike Murphy (mamurph@cs.clemson.edu)
# Revision: 14 November 2008
#
#   Copyright 2007-2008 Clemson University
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
# This material is based upon work supported under a National Science
# Foundation Graduate Research Fellowship. Any opinions, findings,
# conclusions or recommendations expressed in this publication are those of
# the author(s) and do not necessarily reflect the views of the National
# Science Foundation.

# N.B. The base class MachineSourceBase is in stoker_base

from stoker_defines import CFG_USE_LDAP, DFLT_USE_LDAP, EXIT_ERROR_LDAP, \
                           CFG_USE_MYSQL, DFLT_USE_MYSQL, EXIT_ERROR_MYSQL
from stoker_messages import MSG_LDAP_MISSING, MSG_LDAP_ERROR, \
                            MSG_MYSQL_MISSING, MSG_MYSQL_ERROR
from stoker_base import pr_error

# Delay import of stoker_ldap until we know the user wants it
import stoker_local

def get_resolvers(stoker_config):
   resolvers = []
   # Local resolver: always present
   resolvers.append(stoker_local.LocalMachines(stoker_config))
   if stoker_config.getParam(CFG_USE_LDAP, DFLT_USE_LDAP):
      try:
         import stoker_ldap
         resolvers.append(stoker_ldap.LDAPMachines(stoker_config))
      except ImportError, message:
         pr_error([MSG_LDAP_MISSING, message], EXIT_ERROR_LDAP)
      except Exception, message:
         pr_error([MSG_LDAP_ERROR, message], EXIT_ERROR_LDAP)
   if stoker_config.getParam(CFG_USE_MYSQL, DFLT_USE_MYSQL):
      try:
         import stoker_mysql
         resolvers.append(stoker_mysql.MySQLMachines(stoker_config))
      except ImportError, message:
         pr_error([MSG_MYSQL_MISSING, message], EXIT_ERROR_MYSQL)
      except Exception, message:
         pr_error([MSG_MYSQL_ERROR, message], EXIT_ERROR_MYSQL)
   return resolvers

def get_all_targets(resolvers, requested = []):
   targets = []
   if len(requested) < 1:
      requested = list_resolver_names(resolvers)
   # Order matters! Stay in order of resolvers, in case the user has
   # specified this setting on the command line.
   for resolver in resolvers:
      if resolver.CLI_NAME in requested:
         these_targets = resolver.getAllMachines()
         for target in these_targets:
            if target not in targets:
               targets.append(target)
   #
   return targets

def list_resolver_names(resolvers):
   names = []
   for resolver in resolvers:
      if resolver.CLI_NAME != '':
         names.append(resolver.CLI_NAME)
   return names
